package zuk.clipvietfun.plus;

import zuk.clipvietfun.ApiYoutube;
import zuk.clipvietfun.R;
import zuk.clipvietfun.clip.Clip;
import zuk.clipvietfun.clip.ClipAdapter;
import zuk.clipvietfun.likeandhistory.History;
import zuk.clipvietfun.likeandhistory.Like;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class Popup {
	PopupWindow popupWindow;
	Clip video;
	Context context;
	View view;
	ClipAdapter adapter;
	public Popup(Clip clip, FragmentActivity activity, ClipAdapter adapter,
			View view) {
		this.video = clip;
		this.context = activity;
		this.adapter = adapter;
		this.view = view;
	}
	@SuppressWarnings("deprecation")
	public void showPopup(){
		LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
		View popupView = layoutInflater.inflate(R.layout.dialog_settings, null); 
		popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,false);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setTouchable(true);
		Bitmap bm = BitmapFactory.decodeResource(null, R.drawable.bg_popup);
		popupWindow.setBackgroundDrawable(new BitmapDrawable(bm));

		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		int x = (int) ((view.getWidth()-(150*metrics.density))/2);
		int y = (int) ((view.getHeight()/2)*-1);
		Log.e("metrics!!!!!!!!!", Integer.toString(view.getHeight())
				+ " " + Integer.toString(view.getTop())
				+ " " + Integer.toString((int) (200*metrics.density))
				+ " " + y
				+ " " + (int) (view.getTop()+(-y)+200*metrics.density));
		int pHeight = (int) (200*metrics.density);
		if(!(view.getTop()<pHeight))
			y=y-pHeight;
		TextView watch = (TextView)popupView.findViewById(R.id.popup_watch);
		TextView like = (TextView)popupView.findViewById(R.id.popup_like);
		TextView history = (TextView)popupView.findViewById(R.id.popup_history);
		TextView close = (TextView)popupView.findViewById(R.id.popup_close);

		if(popupWindow.isShowing()){
			popupWindow.dismiss();
		}else {
			popupWindow.showAsDropDown(view,x,y);
		}

		//Set Text, Values
		watch.setText("Xem video");
		final boolean isLike,isHistory;
		final Like jLike = new Like(context);
		final History jHistory = new History(context);
		if(new Like(context).isHas(video)){
			like.setText(R.string.removelike);
			isLike = true;
		}else{
			like.setText(R.string.addlike);
			isLike = false;
		}

		if(new History(context).isHas(video)){
			history.setText(R.string.removehistory);
			isHistory = true;
		}else{
			history.setText(R.string.addhistory);
			isHistory = false;
		}
		//Listener
		close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(popupWindow.isShowing())
					popupWindow.dismiss();
			}
		});
		watch.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				watchVideo(video.getuTube());
				close();
			}
		});
		like.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(isLike){
					jLike.removeLike(video);
				}else{
					jLike.addLike(video, false);
				}

				close();
			}
		});
		history.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(isHistory){
					jHistory.removeHistory(video);
				}else{
					jHistory.addHistory(video, false);
					Toast.makeText(context, R.string.addhistory, Toast.LENGTH_SHORT).show();
				}
				close();
			}
		});
	}

	private void watchVideo(String video) {
		Intent i = new Intent(context, ApiYoutube.class);
		i.putExtra("key", video);
		context.startActivity(i);
	}
	private void close() {
		adapter.notifyDataSetChanged();
		if(popupWindow.isShowing())
			popupWindow.dismiss();
	}
}
